package com.hly.basickotlin.base

import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Bundle
import android.os.PersistableBundle
import android.support.v7.app.AppCompatActivity
import butterknife.ButterKnife
import butterknife.Unbinder
import com.githang.statusbar.StatusBarCompat
import com.hly.basickotlin.utils.ActivityManager
import com.hly.basickotlin.utils.HideKey

/**
 * ~~~~~~文件描述:~~~~~~
 * ~~~~~~作者:huleiyang~~~~~~
 * ~~~~~~创建时间:2018/7/13~~~~~~
 * ~~~~~~更改时间:2018/7/13~~~~~~
 * ~~~~~~版本号:1~~~~~~
 *
 */
abstract class BaseActivity : AppCompatActivity() {

    /**
     * 是否禁止旋转屏幕
     */
    private var isAllowScreenRoate = true

    private var unbinder: Unbinder? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        HideKey.init(this) // 键盘点击空白消失
        unbinder = ButterKnife.bind(this)
        if (isCustomBar()) {
            customBar()
        } else {
            StatusBarCompat.setStatusBarColor(this, Color.WHITE)
        }

        if (getLayout() != 0) {
            setContentView(getLayout())
        }
        initView()
        setListener()
        initData()
        ActivityManager.getActivityManager().addActivity(this)

    }

    protected abstract fun getLayout(): Int
    protected abstract fun setListener()
    protected abstract fun initView()
    protected abstract fun initData()


    /**
     * [是否允许屏幕旋转]
     *
     * @param isAllowScreenRoate
     */
    fun setScreenRoate(isAllowScreenRoate: Boolean) {
        this.isAllowScreenRoate = isAllowScreenRoate
    }

    /**
     * 是否自定义状态栏
     *
     * @return
     */
    protected fun isCustomBar(): Boolean {
        return false
    }

    /**
     * 自定义状态栏
     */
    protected fun customBar() {

    }


    override fun onPause() {
        super.onPause()

    }

    override fun onResume() {
        super.onResume()

    }

    override fun onStop() {
        super.onStop()
        HideKey.hideSoftKeyboard(this)
    }

    override fun onDestroy() {
        super.onDestroy()

        if (unbinder != null) {
            unbinder!!.unbind()
        }

        ActivityManager.getActivityManager().finishActivity(this)
    }


}